package cn.sinata.wanglin.mine.fragment;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import com.xilada.xldutils.fragment.RecyclerViewFragment;
import com.xilada.xldutils.utils.DensityUtil;
import com.xilada.xldutils.utils.SharedPreferencesUtils;
import com.xilada.xldutils.utils.Utils;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

import java.util.ArrayList;
import java.util.List;

import cn.sinata.wanglin.R;
import cn.sinata.wanglin.adapter.VillageActiveAdapter;
import cn.sinata.wanglin.commonutils.Constant;
import cn.sinata.wanglin.model.Active;
import cn.sinata.wanglin.net.HttpManager;
import cn.sinata.wanglin.net.model.util.ResultDataSubscriber;
/**
 *我的---小区活动的list.
 *
 */
public class VillageActiveListFragment extends RecyclerViewFragment {
    private List<Active> mActives=new ArrayList<Active>();
    private VillageActiveAdapter mVillageActiveAdapter;
    private int type;
    private int pageNo = 0;
    //（1我参与的0我发布的）
    public static VillageActiveListFragment newInstance(int type){
        VillageActiveListFragment mVillageActiveListFragment = new VillageActiveListFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("type",type);
        mVillageActiveListFragment.setArguments(bundle);
        return mVillageActiveListFragment;
    }

    @Override
    protected RecyclerView.Adapter setAdapter() {
        if (getArguments()!=null){
            type = getArguments().getInt("type");
            Utils.systemErr("---->"+type);
        }
        if(mVillageActiveAdapter==null){

            mVillageActiveAdapter = new VillageActiveAdapter(mActives,getActivity(),type);
        }

        return mVillageActiveAdapter;
    }

    @Override
    protected void onFirstVisibleToUser() {

        if (getArguments()!=null){
            type = getArguments().getInt("type");
        }
        HorizontalDividerItemDecoration itemDecoration = new HorizontalDividerItemDecoration.Builder(context)
                .size(DensityUtil.dip2px(context,10))
                .color(ContextCompat.getColor(context, R.color.dividing_line_color_wanglin))
                .build();
        setItemDecoration(itemDecoration);
        //（1我参与的0我发布的）
       switch (type){
           case 0:
               getMyActivity();
               break;
           case 1:
               getTakeActivity();
               break;
       }
    }

    /**
     * 获取我发布的活动
     */
    private void getMyActivity() {
        String userid= SharedPreferencesUtils.getString(Constant.SP_USERID);
        HttpManager.getMyActivity(userid,pageNo).subscribe(new ResultDataSubscriber<ArrayList<Active>>(this) {
            @Override
            public void onSuccess(String msg, ArrayList<Active> data) {
                if (pageNo == 0) {
                    mActives.clear();
                }
                if (data.size() == 0) {
                    if (pageNo == 0) {
                        setLoadMoreText("暂无数据");
                    } else {
                        pageNo --;
                        setLoadMoreText("没有更多了");
                    }
                }
                mActives.addAll(data);
                mVillageActiveAdapter.notifyDataSetChanged();
            }
        });
    }

    /**
     * 获取我参与的活动
     */
    private void getTakeActivity() {
        String userid= SharedPreferencesUtils.getString(Constant.SP_USERID);
        HttpManager.getTakeActivity(userid,pageNo).subscribe(new ResultDataSubscriber<ArrayList<Active>>(this) {
            @Override
            public void onSuccess(String msg, ArrayList<Active> data) {
                if (pageNo == 0) {
                    mActives.clear();
                }
                if (data.size() == 0) {
                    if (pageNo == 0) {
                        setLoadMoreText("暂无数据");
                    } else {
                        pageNo --;
                        setLoadMoreText("没有更多了");
                    }
                }
                mActives.addAll(data);
                mVillageActiveAdapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    protected void onVisibleToUser() {

    }

    @Override
    protected void onInvisibleToUser() {

    }

    @Override
    protected void pullDownRefresh() {
        super.pullDownRefresh();
        pageNo = 0;
        switch (type){
            case 0:
                getMyActivity();

                break;
            case 1:
                getTakeActivity();
                break;
        }
    }

    @Override
    protected void loadMore() {
        super.loadMore();
        pageNo++;
        switch (type){
            case 0:
                getTakeActivity();
                break;
            case 1:
                getMyActivity();
                break;
        }
    }

}
